package org.example.zhihuan.services;

import org.example.zhihuan.services.projectException.GoodsExistException;
import org.example.zhihuan.services.projectException.GoodsStateException;
import org.example.zhihuan.util.Db;

import java.sql.ResultSet;

//潘思航
public class Goods {

    public static final int GOODS_WAIT = 1;             //商品等待审核
    public static final int GOODS_FAIL = 2;             //商品审核失败
    public static final int GOODS_PASS = 3;             //商品审核通过
    public static final int GOODS_UP = 4;               //商品已上架
    public static final int GOODS_DOWN = 5;             //商品已上架
    public static final int GOODS_DEALING = 6;          //商品交易中
    public static final int GOODS_COMPLETE = 7;         //商品已完成


    private int goodsId;           //商品id
    private String goodsName;     //商品名
    private int userId;             //所属用户id
    private double price;     //商品价格
    private int goodsType;         //商品类型
    private int goodState;        //商品状态     (上架，下架，         审核中，已交易，在售中)
    private String goodsContent;      //商品描述
    private int goodsCollect;      //商品收藏
    private boolean isSaved = false;


    /**
     * Goods读取id
     *
     * @param goodsId
     * @return
     */
    public static boolean goodsIdExist(int goodsId) {
        return Db.read("select 1 from goods where goods_id = " + goodsId, ResultSet::next);
    }

    /**
     * Get商品类
     *
     * @param goodsId
     * @return
     */
    public static Goods getGoods(int goodsId) {
        Goods goods = new Goods();
        Db.read("select * from goods where goods_id = " + goodsId, set -> {
                    if (set.next()) {
                        goods.goodsId = set.getInt("goods_id");
                        goods.userId = set.getInt("user_id");
                        goods.goodsName = set.getString("goods_name");
                        goods.goodState = set.getInt("goods_state");
                        goods.goodsType = set.getInt("goods_type");
                        goods.price = set.getDouble("goods_price");
                        goods.goodsContent = set.getString("goods_content");
                        goods.goodsCollect = set.getInt("goods_collect");
                        goods.isSaved = true;
                    }
                    return null;
                }
        );
        return goods;
    }

    /**
     * 商品创建方法
     *
     * @param goodsName
     * @param goodsType
     * @param price
     * @param goodsContent
     * @param userId
     */
    public static int createGoods(String goodsName, int goodsType, double price, String goodsContent, int userId) {
        Goods goods = new Goods();
        goods.goodsName = goodsName;
        goods.goodsType = goodsType;
        goods.price = price;
        goods.goodsContent = goodsContent;
        goods.userId = userId;
        goods.save();
        return goods.goodsId;
    }

    /**
     * 持久化数据
     */
    private void save() {
        StringBuilder sb = new StringBuilder();
        if (isSaved) {
            sb.append("update goods set user_id = ").append(userId).append(",")
                    .append("goods_name = '").append(goodsName).append("',")
                    .append("goods_state = ").append(goodState).append(",")
                    .append("goods_type = ").append(goodsType).append(",")
                    .append("goods_price = ").append(price).append(",")
                    .append("goods_content = '").append(goodsContent).append("',")
                    .append("goods_collect = ").append(goodsCollect).append(" ")
                    .append(" where goods_id = ").append(goodsId);
        } else {
            sb.append("insert into goods (user_id,goods_name,goods_state,goods_type,goods_price,goods_content,goods_collect) values " + "(")
                    .append(userId).append(",'").append(goodsName).append("',").append(goodState).append(",").append(goodsType)
                    .append(",").append(price).append(",'").append(goodsContent).append("',").append(goodsCollect).append(")");
//            Db.read("select max(goods_id) from goods",set -> {
//                if(set.next()){
//                    this.goodsId=set.getInt(1);
//                }
//                return null;
//            });
            isSaved = true;
        }
        Db.execute(sb.toString());
    }

    /**
     * Goods上架
     */
    public void goodsUp() {
        if (this.goodState == GOODS_PASS) {
            this.goodState = GOODS_UP;
            this.save();
        } else {
            throw new GoodsStateException("商品状态异常");
        }
    }

    /**
     * Goods下架
     */
    public void goodsDown() {
        if (this.goodState == GOODS_UP) {
            this.goodState = GOODS_DOWN;
            this.save();
        } else {
            throw new GoodsStateException("商品状态异常");
        }
    }

    /**
     * Goods修改
     *
     * @param goodsName
     * @param goodsType
     * @param price
     * @param goodsContent
     */
    public void modify(String goodsName, int goodsType, double price, String goodsContent) {
        this.goodsName = goodsName;
        this.goodsType = goodsType;
        this.price = price;
        this.goodsContent = goodsContent;
        this.goodState = GOODS_WAIT;
        this.save();
    }

    /**
     * Goods审核
     *
     * @param check
     */
    public void checkGoods(boolean check) {
        if (this.goodState != GOODS_WAIT) {
            throw new GoodsStateException("商品状态异常");
        }
        //审核通过
        if (check == true) {
                this.goodState = GOODS_PASS;
                this.save();
        //审核不通过
        } else {
                this.goodState = GOODS_FAIL;
                this.save();
            }
    }

    /**
     * Goods交易申请
     */
    public void goodsDealUp() {
        if (this.goodState == GOODS_UP || this.goodState == GOODS_DOWN || this.goodState == GOODS_PASS) {
            this.goodState = GOODS_DEALING;
            this.save();
        } else {
            throw new GoodsStateException("商品状态异常");
        }
    }

    /**
     *Goods交易完成
     */
    public void goodsDealPass() {
        if (this.goodState == GOODS_DEALING ) {
            this.goodState = GOODS_COMPLETE;
            this.save();
        } else {
            throw new GoodsStateException("商品状态异常");
        }
    }

    /**
     * Goods取消
     */
    public void goodsCancel(){
        this.goodState = GOODS_COMPLETE;
        this.save();
    }

    /**
     * Goods交易失败
     */
    public void goodsDealFail() {
        if (this.goodState == GOODS_DEALING) {
            this.goodState = GOODS_PASS;
            this.save();
        } else {
            throw new GoodsStateException("商品状态异常");
        }
    }





}
